//
//  FeedbackViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/14.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "FeedbackViewController.h"

#import "AVRequest+ChangeInfo.h"

@interface FeedbackViewController () <UITextViewDelegate>

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *sure;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAppearance];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - initialize

- (void)initializeAppearance {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBar.title.text = @"意见反馈";
    [self.view addSubview:self.label];
    [self.view addSubview:self.textView];
    [self.view addSubview:self.sure];
}

#pragma mark - URLRequest

- (void)urlRequest {
    [AVRequest avFeedbackWithContent:self.textView.text aSucceed:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"请求失败" message:nil];
            return;
        }
        
        if (succeed) {
            self.textView.text = @"";
            [Alert alertWithTitle:@"反馈成功" message:nil];
        }else {
            [Alert alertWithTitle:@"请求失败" message:nil];
        }
        
    }];
}

#pragma mark - respondsTo...

- (void)respondsToSure:(UIButton *)sender {
    if (self.textView.text.length <= 10) {
        [Alert alertWithTitle:@"亲，意见反馈不能少于十个字" message:nil];
    }else {
        [self urlRequest];
    }
}

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if (textView.text.length >= 200) {
        return NO;
    }else {
        return YES;
    }
    
}

#pragma mark - get

- (UILabel *)label {
    
    if (!_label) {
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 64, kWIDTH - 40, 40)];
        _label.font = [UIFont systemFontOfSize:16];
        _label.text = @"您的意见，是我们最大的动力";
        
    }
    return _label;
}

- (UITextView *)textView {
    
    if (!_textView) {
        
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.label.frame), kWIDTH - 40, WXQAdaptionHeight(230))];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:14];
        _textView.layer.cornerRadius = 10;
        _textView.layer.borderWidth = 1;
        _textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
    }
    return _textView;
}

- (UIButton *)sure {
    
    if (!_sure) {
        
        _sure = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.textView.frame), CGRectGetMaxY(self.textView.frame) + 20, CGRectGetWidth(self.textView.bounds), 50)];
        _sure.layer.cornerRadius = 5;
        _sure.backgroundColor = [UIColor orangeColor];
        [_sure setTitle:@"提交意见" forState:UIControlStateNormal];
        [_sure addTarget:self action:@selector(respondsToSure:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _sure;
}

@end











